﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Aprendete.Models
{
    public class SalaRepository
    {

        private DataClasses1DataContext db = new DataClasses1DataContext();

        public void Add(Sala Sala)
        {
            db.Salas.InsertOnSubmit(Sala);
            db.SubmitChanges();
        }

        public void Usar(int salaID)
        {
            GetSalaxid(salaID).Usada = true;            

            db.SubmitChanges();
        }

        public void Save()
        {
            db.SubmitChanges();
        }


        public Sala GetSalaxid(int id)
        {
            return db.Salas.SingleOrDefault(d => d.SalaID == id);
        }

        public Sala GetSalaxLink(string link)
        {
            return db.Salas.SingleOrDefault(d => d.Link == link);
        }

        public Sala GetSalaLibre()
        {
            try
            {
                return db.Salas.First(d => d.Usada == false);
            }
            catch 
            {
                return null;
            }
        }

        public void Delete(Sala Sala)
        {
            db.Salas.DeleteOnSubmit(Sala);
            db.SubmitChanges();
        }

    }
}